package mbsl.books;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import mbsl.commons.base.ElementViewDialog;
import mbsl.system.TextProvider;
import mbsl.ui.Slider;
import static mbsl.books.Book.COLOUR;
import static mbsl.commons.base.ElementController.RATING_MAX;

class BookViewDialog extends ElementViewDialog {
	private TextProvider tp = Books.tp.prefix("books.edit");

	BookViewDialog(Shell pParentFrame) {
		super(pParentFrame, null, COLOUR);
		
		addLabel(tp.get("author") + ":");
		mAuthor = addInfo();
		addLabel(tp.get("genre") + ":");
		mGenre = addInfo();
		addLabel(tp.get("year") + ":");
		mYear = addInfo();
		addLabel(tp.get("location") + ":");
		mLocation = addInfo();
		addLabel(tp.get("format") + ":");
		mFormat = addInfo();
		addLabel(tp.get("rating") + ":");
		mRating = new Slider(getInfoPanel(), RATING_MAX, SWT.None);
		
		GridData sliderGridData = new GridData(300, 20);
		
		mRating.setLayoutData(sliderGridData);

		mRating.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent pE) {
				mRating.setEnabled(false);
			}
		});
		
		getWindow().addShellListener(new ShellAdapter() {
			public void shellClosed(ShellEvent pE) {
				pE.doit = false; // Signals that the system shouldn't continue with the closing operation
				
				getWindow().setVisible(false);
			}
		});
	}
	
	public void setTitle(String pTitle) {
		super.setTitle(pTitle);
	}
	public void setAuthor(String pAuthor) {
		if (pAuthor != null && !pAuthor.isEmpty())
			mAuthor.setText(pAuthor);
	}
	public void setFormat(String pFormat) {
		if (pFormat != null && !pFormat.isEmpty())
			mFormat.setText(pFormat);
	}
	void setYear(String pYear) {
		if (pYear != null && !pYear.isEmpty())
			mYear.setText(pYear);
	}
	void setLocation(String pLocation) {
		if (pLocation != null && !pLocation.isEmpty())
			mLocation.setText(pLocation);
	}
	void setGenre(String pGenre) {
		if (pGenre != null && !pGenre.isEmpty())
			mGenre.setText(pGenre);
	}
	void setRating(int pRating) {
		mRating.setValue(pRating);

		if (pRating > 0) {
			mRating.setEnabled(false);
			mRating.setToolTipText(String.format("%d", pRating));
		} else
			mRating.setEnabled(true);
	}
	public Slider getRating() {
		return mRating;
	}

	private Label mGenre;
	private Label mYear;
	private Label mLocation;
	private Slider mRating;
	private Label mAuthor;
	private Label mFormat;
}
